﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.Abstract;
using Business.Service;
using PresentationMVC.Models;
using Domain.Entity;
using Business.Common;

namespace PresentationMVC.Controllers
{
    public class InvoiceController : Controller
    {
        //
        // GET: /Invoice/

        #region Action
        public ActionResult Index(InvoiceDataModel model)
        {
            Session.Remove(Constants.INVOICE_SEARCH_SESSION);
                      
            model = GetInvoiceDataModel(model);

            InvoiceSearchModel ism = new InvoiceSearchModel();
            ism.FromDate = model.FromDate;
            ism.ToDate = model.ToDate;

            List<JobBooking> lst = new List<JobBooking>();
            model.JobBookingByInvoiceId = lst; 

            model.InvoiceSearch = ism;

            return View(model);
        }

        public ActionResult GridviewSearch(InvoiceDataModel model)
        {
            Session[Constants.INVOICE_SEARCH_SESSION] = model.InvoiceSearch;
            this.ViewData.Model = GetInvoiceDataModel(model);
            return PartialView("InvoiceGridPartial");
        }

        [HttpGet]
        public ActionResult ReloadGrid(InvoiceDataModel model)
        {
            model = GetInvoiceDataModel(model);
            this.ViewData.Model = model;
            return PartialView("InvoiceGridPartial", model);
        }

        public ActionResult CreateInvoice()
        {
            return View(new InvoiceDataModel());
        }
               
        //[HttpGet]
        public ActionResult ViewInvoice(long id)
        {
            IJobBookingService srv = new JobBookingServiceImpl();
            IList<JobBooking> jobLst = srv.GetJobBookingByInvoiceId(id);
            InvoiceDataModel model = new InvoiceDataModel();
            model.JobBookingByInvoiceId = jobLst;

            return PartialView("InvoiceJobBookingGridPartial", model);
        }
        #endregion

        #region Private Methods

        private InvoiceDataModel GetInvoiceDataModel(InvoiceDataModel model)
        {
            return GetInvoiceDataModel(model, null);
        }

        private InvoiceDataModel GetInvoiceDataModel(InvoiceDataModel model, Invoice invoice)
        {
            IInvoiceService srv = new InvoiceServiceImpl();
            IList<Invoice> lst = new List<Invoice>();
           
            if (Session[Constants.INVOICE_SEARCH_SESSION] == null)
            {
                lst = srv.GetAllInvoices(DateTime.Parse(model.FromDate), DateTime.Parse(model.ToDate)); ;
            }
            else
            {
                InvoiceDataModel idm = new InvoiceDataModel();
                idm.InvoiceSearch = Session[Constants.INVOICE_SEARCH_SESSION] as InvoiceSearchModel;

                if (idm.InvoiceSearch.CustomerID > 0)
                {
                    lst = srv.GetAllInvoices(idm.InvoiceSearch.CustomerID, DateTime.Parse(idm.InvoiceSearch.FromDate), DateTime.Parse(idm.InvoiceSearch.ToDate));
                }
                else
                    lst = srv.GetAllInvoices(DateTime.Parse(idm.InvoiceSearch.FromDate), DateTime.Parse(idm.InvoiceSearch.ToDate));
               
            }

            InvoiceDataModel m = new InvoiceDataModel();
            m.Invoice = invoice == null ? new Invoice() : invoice;
            model.AllInvoice = lst;

            return model;
        }

        #endregion
    }
}
